/*
 * Copyright @ 2024 weili23
 * com.example.mythread.a08threadExerciseOddNumber 15:54
 * All right reserved.
 */

package com.example.mythread.a08threadExerciseOddNumber;

/**
 * @desc: 获取1-00之间的奇数
 * @author: weili23
 * @createTime: 2024/10/21 15:54
 * @version: v1.0
 */
public class OddNumber implements Runnable{
    private static int max = 100;
    private static int currentNumber = 1;
    private static Object lock = new Object();
    @Override
    public void run() {
        while(currentNumber<=max){
           synchronized (lock){
               if(currentNumber<=max){
                   if(currentNumber %2 != 0){
                       System.out.println(Thread.currentThread().getName()+":"+currentNumber);
                       currentNumber++;
                       lock.notifyAll();
                   }else{
                       try {
                           currentNumber++;
                           lock.wait();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }else{
                   System.out.println("结束");
                   break;
               }

           }
        }
    }


}
